c# sockets

165

// A C# program for Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
namespace Client {
 
class Program {
 
// Main Method
static void Main(string[] args)
{
    ExecuteClient();
}
 
// ExecuteClient() Method
static void ExecuteClient()
{
 
    try {
         
        // Establish the remote endpoint
        // for the socket. This example
        // uses port 11111 on the local
        // computer.
        IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddr = ipHost.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 11111);
 
        // Creation TCP/IP Socket using
        // Socket Class Constructor
        Socket sender = new Socket(ipAddr.AddressFamily,
                   SocketType.Stream, ProtocolType.Tcp);
 
        try {
             
            // Connect Socket to the remote
            // endpoint using method Connect()
            sender.Connect(localEndPoint);
 
            // We print EndPoint information
            // that we are connected
            Console.WriteLine("Socket connected to -> {0} ",
                          sender.RemoteEndPoint.ToString());
 
            // Creation of message that
            // we will send to Server
            byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
            int byteSent = sender.Send(messageSent);
 
            // Data buffer
            byte[] messageReceived = new byte[1024];
 
            // We receive the message using
            // the method Receive(). This
            // method returns number of bytes
            // received, that we'll use to
            // convert them to string
            int byteRecv = sender.Receive(messageReceived);
            Console.WriteLine("Message from Server -> {0}",
                  Encoding.ASCII.GetString(messageReceived,
                                             0, byteRecv));
 
            // Close Socket using
            // the method Close()
            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
        }
         
        // Manage of Socket's Exceptions
        catch (ArgumentNullException ane) {
             
            Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
        }
         
        catch (SocketException se) {
             
            Console.WriteLine("SocketException : {0}", se.ToString());
        }
         
        catch (Exception e) {
            Console.WriteLine("Unexpected exception : {0}", e.ToString());
        }
    }
     
    catch (Exception e) {
         
        Console.WriteLine(e.ToString());
    }
}
}
}

Comments

Submit
0 Comments

More Questions

cmd cant change directoryhow to open d drive using conda prompt how to add basic authentication on haproxy backend server
cmd cd not workingvideo player html express hello world
form submit with file upload in phpcreate latex document button avalonia
simple deployment diagram for basic crud operationsc# sockets guzzlehttp authorization basic
using axios get with authorizationpehkui mod forge c# restclient get basic authentication
curl authJASON WEB TOKENS EM VBNET COM WINDOWS FORMS git commands
c64 basic define variableauthentication in fetch post how to create authentication and authorization in local api angular 14
basic array questionsc++ code snippets what are the 3 basic types of plate boundaries explain their differences (how they act).
pandas column statisticshow to make a calculator in python pygame window
raylib c# basic windowsmall basic textwindow npm err! code e401 npm err! unable to authenticate, need: basic realm=https:--pkgsprodeus21.pkgs.visualstudio.com-
split a column in google sheetshow to serve html file in node.js vba check if range is empty
create route in laravel 8golang http post authentication lib in tsconfig
range recursion javascriptvbs error message how to make an exit button using vs code
how to delete a value from document in mongodbvb.net remove case sensitivity face recognition python opencv haar cascade
curl to upload to artifactoryunity simple movement script node-fetch auth
basic symbol meanings in c++visual basic variable declaration visual basic run command